package com.lgc;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class EmailApplication {
    static String username = "liguancheng201105";  // 不用加@126.com，网站自动补充
    static String password = "li13229014730";
    static String receiver = "2960853985@qq.com";
    static String topic = "测试";
    static String contentText = "This is a test email.";

    public static void main(String[] args) throws InterruptedException {

        // 设置驱动程序的路径
        System.setProperty("webdriver.chrome.driver", "D:\\workspace\\selenium4QQDemo\\src\\main\\resources\\chromedriver.exe");
        // 创建Chrome浏览器驱动
        ChromeDriver driver = new ChromeDriver();
        // 前往邮箱地址
        driver.get("https://mail.126.com/");

        // 进入第一次frame
        WebElement iframe = driver.findElement(By.tagName("iframe"));
        driver.switchTo().frame(iframe);

        // 点击输入账号密码
        driver.findElement(By.xpath("//input[@name='email']")).sendKeys(username);
        driver.findElement(By.xpath("//input[@id='pwdtext']")).sendKeys(password);

        // 点击登录按钮
        driver.findElement(By.xpath("//*[@id='dologin']")).click();

        // 等待登录成功并跳转到邮箱主页,少情况出现了验证方式，要手动验证
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
        wait.until(ExpectedConditions.urlContains("welcome.WelcomeModule"));

        // 切换成默认主窗口   ！！！！
        driver.switchTo().defaultContent();

        // 点击写信
        driver.findElement(By.xpath("/html/body/div/nav/div/ul/li[@id='_mail_component_74_74']")).click();

        // 进入 写信界面
        // 输入收信人 和 主题
        driver.findElement(By.xpath("//input[@class='nui-editableAddr-ipt']")).sendKeys(receiver);
        driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[2]/div[1]/div/div/input")).sendKeys(topic);

        // 进入正文frame
        WebElement contentInput = driver.findElement(By.xpath("//iframe[@class='APP-editor-iframe']"));
        driver.switchTo().frame(contentInput);

        // 输入正文
        WebElement contentBody = driver.findElement(By.xpath("/html/body"));
        contentBody.sendKeys(contentText);

        // 切回主窗口
        driver.switchTo().defaultContent();

        // 点击发送按钮
        driver.findElement(By.xpath("//span[text()='发送']")).click();

        // 等待发送完成
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 关闭浏览器
        // driver.quit();
    }


    /**
     * 获取驱动程序，注意检查 驱动路径是否正确
     * 版本号： 118.xx
     * 其他版本号： https://googlechromelabs.github.io/chrome-for-testing/#stable
     * 参考文章：https://blog.csdn.net/Z_Lisa/article/details/133307151
     *
     * @return
     */
    private static ChromeDriver getChromeDriver() {
        // 设置驱动程序的路径
        System.setProperty("webdriver.chrome.driver", "D:\\workspace\\selenium4QQDemo\\src\\main\\resources\\chromedriver.exe");
        // 创建Chrome浏览器驱动
        ChromeDriver driver = new ChromeDriver();
        // 前往邮箱地址
        driver.get("https://mail.126.com/");
        return driver;
    }
}
